package ${package}.service.impl;

import ${package}.domain.${className};
<#if columns??>
    <#list columns as column>
        <#if column.columnKey = 'UNI'>
            <#if column_index = 1>
import me.zhengjie.exception.EntityExistException;
            </#if>
        </#if>
    </#list>
</#if>
import ${package}.repository.${className}Repository;
import ${package}.service.I${className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import org.springframework.data.domain.*;
import javax.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Field;
import ${package}.common.NotCondition;
import  ${package}.common.query.${className}Query;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

/**
* @author ${author}
* @date ${date}
*/
@Service
public class ${className}ServiceImpl implements I${className}Service {

    @Autowired
    private ${className}Repository ${changeClassName}Repository;


    @Override
    public ${className} findById(${pkColumnType} id) {
        Optional<${className}> optional = ${changeClassName}Repository.findById(id);
        if(optional.isPresent()){
            return  optional.get();
        }else{
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${className} create(${className} resources) {
<#if columns??>
    <#list columns as column>
    <#if column.columnKey = 'UNI'>
        if(${changeClassName}Repository.findBy${column.capitalColumnName}(resources.get${column.capitalColumnName}()) != null){
            throw new EntityExistException(${className}.class,"${column.columnName}",resources.get${column.capitalColumnName}());
        }
    </#if>
    </#list>
</#if>
        return ${changeClassName}Repository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(${className} resources) {
        Optional<${className}> optional = ${changeClassName}Repository.findById(resources.getId());
        if(optional.isPresent()){
            ${className} ${changeClassName} = optional.get();
            <#if columns??>
                <#list columns as column>
                    <#if column.columnKey = 'UNI'>
                        <#if column_index = 1>
                            ${className} ${changeClassName}1 = null;
                        </#if>
                        ${changeClassName}1 = ${changeClassName}Repository.findBy${column.capitalColumnName}(resources.get${column.capitalColumnName}());
                    if(${changeClassName}1 != null && !${changeClassName}1.getId().equals(${changeClassName}.getId())){
                        throw new EntityExistException(${className}.class,"${column.columnName}",resources.get${column.capitalColumnName}());
                    }
                    </#if>
                </#list>
            </#if>
                    // 此处需自己修改
                    resources.setId(${changeClassName}.getId());
            ${changeClassName}Repository.save(resources);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        ${changeClassName}Repository.deleteById(id);
    }

    @Override
    public Page<${className}> getAll(${className}Query query, Pageable pageable) {
        return this.getDataList(query, pageable);
    }

    private Page<${className}> getDataList(${className}Query query,Pageable pageable) {
    Specification<${className}> querySpecifi = new Specification<${className}>() {
        public Predicate toPredicate(Root<${className}> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            List<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(query.getBeginDate())) {
                    //大于或等于传入时间
                    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), query.getBeginDate()));
                }
                if (!StringUtils.isEmpty(query.getEndDate())) {
                    //小于或等于传入时间
                    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), query.getEndDate()));
                }
                if (query != null) {
                Class<? extends ${className}Query> clazz = query.getClass();
                Field[] fields = clazz.getDeclaredFields();
                for (Field tmpField : fields) {
                    tmpField.setAccessible(true);
                    try {
                        NotCondition annotation = (NotCondition) tmpField.getDeclaredAnnotation(NotCondition.class);
                        if(tmpField.get(query)!=null && annotation==null)
                        {
                           String name = tmpField.getName();
                           predicates.add(criteriaBuilder.equal(root.get(name), tmpField.get(query)));
                        }
                        } catch (IllegalAccessException e) {
                             e.printStackTrace();
                        }
                    }
                }
                // and到一起的话所有条件就是且关系，or就是或关系
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return  ${changeClassName}Repository.findAll(querySpecifi,pageable);
    }
}